<?php
namespace App\Model;

use PhalApi\Model\NotORMModel as NotORM;

class Order extends NotORM {

    //设置表名
    protected function getTableName($id) {
        return 'order';
    }

    // 获取我的普通订单列表
    public function getOrderList( $user_id, $curpage, $where ) {

        $sql = 'SELECT o.order_id, o.user_id, o.order_sn, o.total_amount, o.order_status, o.shipping_status, o.pay_status'
            . ' FROM tp_order AS o'
            . ' WHERE o.pintuan_status = 1 AND o.user_id = '.$user_id.$where
            . ' ORDER BY o.add_time DESC'
            . ' limit '.$curpage.',10';
        return $this->getORM()->queryAll($sql, array());
    }

    // 获取我的普通订单列表总和
    public function getOrderListCount( $user_id, $where ) {

        $sql = 'SELECT COUNT(o.order_id) as all_num'
            . ' FROM tp_order AS o'
            . ' WHERE o.pintuan_status = 1 AND o.user_id = '.$user_id.$where;
        return $this->getORM()->queryAll($sql, array());
    }

    // 订单详情
    public function getOrderDetail( $user_id, $order_id ) {
        return $this->getORM()
            ->select('order_sn,user_id,order_id,order_status,shipping_status,pay_status,consignee,province,city,district,address,mobile,coupon_price,shipping_code,total_amount,add_time')
            ->where( array( 'user_id'=>$user_id, 'order_id'=>$order_id) )
            ->fetchOne();
    }

    // 商品详情
    public function getOrderGoodsDetail( $order_id ) {
        $orm = \PhalApi\DI()->notorm->order_goods;
        return $orm
            ->select('goods_id,spec_key,goods_price,add_time,goods_name,goods_num,final_price,spec_key_name,goods_images,delivery_id,prom_type')
            ->where( array( 'order_id'=>$order_id) )
            ->fetchAll();
    }

    // 快递详情
    public function getOrderGoodsDelivery( $delivery_id ) {
        $orm = \PhalApi\DI()->notorm->delivery_doc;
        return $orm
            ->select('shipping_code,invoice_no,shipping_name')
            ->where( array( 'id'=>$delivery_id) )
            ->fetchOne();
    }

    // 获取我参与的拼团列表
    public function getMyAssembleOrder( $user_id, $curpage ) {

        $sql = 'SELECT o.add_time, o.user_id, o.order_id, o.order_prom_id, o.is_myself, og.spec_key_name, og.final_price, f.found_id, f.found_end_time, f.team_id, `join`, need, (need - `join`) as few, f.goods_id, f.status,og.goods_images'
            . ' FROM tp_order AS o LEFT JOIN tp_order_goods AS og'
            . ' ON o.order_id = og.order_id'
            . ' LEFT JOIN tp_team_found AS f'
            . ' ON o.order_prom_id = f.found_id'
            . ' WHERE o.order_prom_id != 0 AND o.user_id = '.$user_id
            . ' ORDER BY o.add_time DESC'
            . ' limit '.$curpage.',10';
        return $this->getORM()->queryAll($sql, array());
    }

    // 获取我参与的拼团列表总和
    public function getMyAssembleOrderCount( $user_id ) {

        $sql = 'SELECT COUNT(o.order_id) as all_num'
            . ' FROM tp_order AS o LEFT JOIN tp_order_goods AS og'
            . ' ON o.order_id = og.order_id'
            . ' LEFT JOIN tp_team_found AS f'
            . ' ON o.order_prom_id = f.found_id'
            . ' WHERE o.user_id = '.$user_id;
        return $this->getORM()->queryAll($sql, array());
    }

    //订单退款申请
    public function returnOrder( $order_id ) {
        return $this->getORM()
            ->where(array( 'order_id'=>$order_id ))
            ->update(array('order_status' => 2));
    }

    //订单评论
    public function comment( $data ) {
        $orm = \PhalApi\DI()->notorm->comment;
        $orm->insert( $data );
        return $orm->insert_id();
    }

    //插入临时表的订单数据
    public function saveOrder( $data ) {
        $orm = \PhalApi\DI()->notorm->order_temporary;
        $orm->insert( $data );
        return $orm->insert_id();
    }

    // 购买时进行库存的变更
    public function changeSpecNum( $goods_id, $spec_key, $goods_num, $act = '1' ) {//1为减，2为加
        $orm = \PhalApi\DI()->notorm->spec_goods_price;
        if( $act == 1 ){
            // 返回影响的行数
            return $orm
                ->where(array( 'goods_id'=>$goods_id, '`key`' => $spec_key ))
                ->update(array('store_count' => new \NotORM_Literal("store_count - $goods_num")));
        }else{
            // 返回影响的行数
            return $orm
                ->where(array( 'goods_id'=>$goods_id, '`key`' => $spec_key ))
                ->update(array('store_count' => new \NotORM_Literal("store_count + $goods_num")));
        }
    }

    // 购买时进行库存的变更
    public function changeGoodsSaleNum( $goods_id, $goods_num ) {
        $orm = \PhalApi\DI()->notorm->goods;
            // 返回影响的行数
            return $orm
                ->where(array( 'goods_id'=>$goods_id ))
                ->update(array('virtual_sales_sum' => new \NotORM_Literal("virtual_sales_sum + $goods_num")));
    }

    // 购买时进行库存的变更
    public function changeGoodsNum( $goods_id, $goods_num, $act = 1 ) {//1为减，2为加
        $orm = \PhalApi\DI()->notorm->goods;
        if( $act == 1 ){
            // 返回影响的行数
            return $orm
                ->where(array( 'goods_id'=>$goods_id ))
                ->update(array('store_count' => new \NotORM_Literal("store_count - $goods_num")));
        }else{
            // 返回影响的行数
            return $orm
                ->where(array( 'goods_id'=>$goods_id ))
                ->update(array('store_count' => new \NotORM_Literal("store_count + $goods_num")));
        }
    }

    // 查询订单是否创建成功
    public function orderVerify( $order_sn ) {
        return $this->getORM()
            ->select('*')
            ->where( array( 'order_sn'=>$order_sn ) )
            ->fetchOne();
    }

    // 查询订单是否创建成功
    public function orderTemporary( $order_sn, $is_child = 1 ) {//1查询主订单 2查询子订单
        $orm = \PhalApi\DI()->notorm->order_temporary;
        if( $is_child == 1 ){
            return $orm
                ->select('*')
                ->where( array( 'order_sn'=>$order_sn ) )
                ->fetchOne();
        }else{
            return $orm
                ->select('*')
                ->where( array( 'parent_ordersn'=>$order_sn ) )
                ->fetchAll();
        }
    }

    // 获取我的普通订单列表
    public function getAllOrder() {
        $sql = 'SELECT o.order_id, o.user_id, o.order_sn, o.total_amount, o.order_status, o.shipping_status, o.pay_status'
            . ' FROM tp_order AS o'
            . ' WHERE o.shipping_status = 1 AND o.order_status = 1'
            . ' ORDER BY o.add_time DESC';
        return $this->getORM()->queryAll($sql, array());
    }

    //订单退款申请
    public function changeOrderStatus( $order_id ) {
        return $this->getORM()
            ->where(array( 'order_id'=>$order_id ))
            ->update(array('order_status' => 5));
    }

    // 获取全部暂未拼成的订单
    public function getAllFound() {
        $orm = \PhalApi\DI()->notorm->team_found;
        return $orm
            ->select('*')
            ->where( array( 'status'=>1 ) )
            ->fetchAll();
    }

    // 获取全部暂未拼成的订单
    public function getConFollow( $found_id ) {
        $orm = \PhalApi\DI()->notorm->team_follow;
        return $orm
            ->select('*')
            ->where( array( 'found_id'=>$found_id ) )
            ->fetchAll();
    }

    // 获取拼团成功的订单及商品信息
    public function getAllFoundOrder() {
        $sql = 'SELECT o.add_time, o.order_sn, o.user_id, o.order_id, o.order_prom_id, o.is_myself, og.spec_key_name, og.goods_price,og.final_price, f.found_id, f.found_end_time, f.team_id, `join`, need, (need - `join`) as few, f.goods_id, f.status,og.goods_images,og.goods_name, ot.form_id, ot.openid'
            . ' FROM tp_order AS o LEFT JOIN tp_order_goods AS og'
            . ' ON o.order_id = og.order_id'
            . ' LEFT JOIN tp_team_found AS f'
            . ' ON o.order_prom_id = f.found_id'
            . ' LEFT JOIN tp_order_temporary AS ot'
            . ' ON ot.order_sn = o.order_sn'
            . ' WHERE o.is_send = 0 AND f.status = 2 AND ot.is_success = 2'
            . ' ORDER BY o.add_time DESC';
        return $this->getORM()->queryAll($sql, array());
    }

    //更改订单推送状态
    public function changeOrderSend( $order_sn ) {
        return $this->getORM()
            ->where(array( 'order_sn'=>$order_sn ))
            ->update(array('is_send' => 1));
    }

    // 查询所有未推送的退款订单
    public function getAllReturnOrder() {
        $sql = 'SELECT o.goods_name,o.order_sn,o.refund_amount,o.admin_message, ot.form_id, ot.openid'
            . ' FROM tp_order_refund AS o LEFT JOIN tp_order_temporary AS ot'
            . ' ON o.order_sn = ot.order_sn'
            . ' WHERE o.is_send = 0 AND ot.is_success = 2'
            . ' ORDER BY o.add_time DESC';
        return $this->getORM()->queryAll($sql, array());
    }

    //更改退款订单推送状态
    public function changeReturnOrderSend( $order_sn ) {
        $orm = \PhalApi\DI()->notorm->order_refund;
        return $orm
            ->where(array( 'order_sn'=>$order_sn ))
            ->update(array('is_send' => 1));
    }

    // 查询所有未推送的发货中的订单
    public function getAllShippingOrder() {
        $sql = 'SELECT o.add_time, o.order_sn, o.user_id, o.order_id, o.order_prom_id,o.order_amount,o.province,o.city,o.district,o.address, o.is_myself, og.spec_key_name, og.goods_price,og.final_price,og.goods_images,og.goods_name, ot.form_id, ot.openid'
            . ' FROM tp_order AS o LEFT JOIN tp_order_goods AS og'
            . ' ON o.order_id = og.order_id'
            . ' LEFT JOIN tp_order_temporary AS ot'
            . ' ON ot.order_sn = o.order_sn'
            . ' WHERE o.is_send = 0 AND ot.is_success = 2 AND o.shipping_status = 1'
            . ' ORDER BY o.add_time DESC';
        return $this->getORM()->queryAll($sql, array());
    }

    //更改found状态
    public function changeFoundFail( $found_id ) {
        return $this->getORM()
            ->where(array( 'found_id'=>$found_id ))
            ->update(array('status' => 3));
    }

    //更改follow状态
    public function changeFollowFail( $follow_id ) {
        return $this->getORM()
            ->where(array( 'follow_id'=>$follow_id ))
            ->update(array('status' => 3));
    }

    // 根据order_id查询临时表的商品数据
    public function getTemporaryById( $order_id ) {
        $sql = 'SELECT ot.goods_info'
            . ' FROM tp_order AS o LEFT JOIN tp_order_temporary AS ot'
            . ' ON o.order_sn = ot.order_sn'
            . ' WHERE o.order_id = '.$order_id
            . ' ORDER BY o.add_time DESC';
        return $this->getORM()->queryAll($sql, array());
    }
}
